using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class TutorialTouchView : MonoBehaviour
{
	public RectTransform canvasRect;

	public Image trailHead;

	public Image trailTail;

	public Image hand;

	private Vector2 trailInitialSize;

	private RectTransform rectTransform;

	private Tweener tween;

	private Vector3 prevPosition;

	private float speed;

	private float angle;

	private bool showingHand = true;

	private void Start()
	{
	}

	private void OnEnable()
	{
		Init();
	}

	private void Init()
	{
		rectTransform = GetComponent<RectTransform>();
		trailInitialSize = trailHead.rectTransform.sizeDelta;
		trailHead.CrossFadeAlpha(0f, 0f, true);
		trailTail.CrossFadeAlpha(0f, 0f, true);
		hand.CrossFadeAlpha(0f, 0f, true);
	}

	public void ShowSwipe(Vector3 startPos, Vector3 endPos, float duration, bool showHand = true)
	{
		if (tween != null)
		{
			tween.Kill();
			tween = null;
		}
		showingHand = showHand;
		startPos.x *= canvasRect.sizeDelta.x;
		startPos.y *= canvasRect.sizeDelta.y;
		endPos.x *= canvasRect.sizeDelta.x;
		endPos.y *= canvasRect.sizeDelta.y;
		rectTransform.anchoredPosition = startPos;
		prevPosition = base.transform.localPosition;
		tween = DOTween.To(() => rectTransform.anchoredPosition, delegate(Vector3 x)
		{
			rectTransform.anchoredPosition = x;
		}, endPos, duration).SetEase(Ease.InSine).OnStart(OnSwipeStart)
			.OnUpdate(OnSwipeUpdate)
			.OnComplete(OnSwipeComplete);
	}

	private void OnSwipeStart()
	{
		trailHead.CrossFadeAlpha(1f, 0.05f, true);
		trailTail.CrossFadeAlpha(1f, 0.05f, true);
		if (showingHand)
		{
			hand.CrossFadeAlpha(1f, 0.05f, true);
		}
		speed = 0f;
		angle = 0f;
		SetTrail();
	}

	private void OnSwipeComplete()
	{
		trailHead.CrossFadeAlpha(0f, 0.1f, true);
		trailTail.CrossFadeAlpha(0f, 0.1f, true);
		hand.CrossFadeAlpha(0f, 0.15f, true);
	}

	private void OnSwipeUpdate()
	{
		if (base.transform.localPosition != prevPosition)
		{
			angle = Vector3.Angle(Vector3.up, base.transform.localPosition - prevPosition);
			if (base.transform.localPosition.x < prevPosition.x)
			{
				angle *= -1f;
			}
		}
		speed = Vector3.Distance(prevPosition, base.transform.localPosition) / Time.deltaTime;
		speed = Mathf.Clamp(speed, 0f, 10000f);
		prevPosition = base.transform.localPosition;
		SetTrail();
	}

	private void SetTrail()
	{
		float num = 0.005f;
		float y = trailInitialSize.y + speed * trailInitialSize.y * num;
		trailHead.rectTransform.localEulerAngles = Vector3.forward * (0f - angle);
		trailTail.rectTransform.sizeDelta = new Vector2(trailInitialSize.x, y);
	}
}
